/*
 * @Author: liutongchao 1923236391@qq.com
 * @Date: 2023-05-27 13:33:45
 * @LastEditors: liutongchao 1923236391@qq.com
 * @LastEditTime: 2023-06-25 22:08:47
 * @FilePath: \greenbeeadmin\controllers\admin\user.go
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package controllers

import (
	"encoding/json"
	adminService "greenbeeadmin/controllers/admin/service"
	greenbee "greenbeeadmin/controllers/greenbee"
	greenbeeUtils "greenbeeadmin/controllers/greenbee/utils"
	"greenbeeadmin/models"
	"strings"
)

// admin用户管理
type AdminUserController struct {
	greenbee.AdminBaseController
}

// 用户列表页面
func (c *AdminUserController) Get() {
	c.TplName = "admin/user/list.html"
}

/**
 * @description: 获取用户列表数据接口
 * @return {*}
 */
func (c *AdminUserController) Post() {
	var data []models.AdminUser
	page := c.GetString("page", "")
	limit := c.GetString("limit", "")
	searchParam := c.GetString("searchParams", "")

	params := make(map[string]string)
	var userService adminService.AdminUserService
	if searchParam != "" {
		bit := []byte(searchParam)
		var searchParams adminService.AdminUserSearchParams
		json.Unmarshal(bit, &searchParams)
		params["username"] = searchParams.Username
	}
	if page != "" {
		params["page"] = page
	}
	if limit != "" {
		params["limit"] = limit
	}

	//查询用户数据
	fields := make([]string, 0)
	fields = append(fields, "*")
	data, total_row, err := userService.GetList(params, fields)
	if err == nil {
		c.SuccessJson(0, data, int(total_row))
	}

	c.ErrorJson(500, "获取失败", 0, data)
}

// 删除后台用户 struct
type DelAdminUserController struct {
	greenbee.AdminBaseController
}

/**
 * @description: 删除用户
 * @return {*}
 */
func (c *DelAdminUserController) Post() {
	var data interface{}
	ids := c.GetStrings("ids[]")
	id, _ := c.GetInt("id", 0)
	action := c.GetString("action", "")

	var num int64
	var userService adminService.AdminUserService
	if action == "dels" {
		//批量删除
		if len(ids) <= 0 {
			c.ErrorJson(500, "参数有误", 0, data)
		}
		num = userService.UserDeletes(ids)
	} else {
		//单个删除
		if id == 0 {
			c.ErrorJson(500, "参数有误", 0, data)
		}
		//判断用户账号是否为超级用户
		var user models.AdminUser
		user.Id = id
		field := []string{"Id"}
		user, err := userService.GetUser(user, field)
		if err != nil {
			c.ErrorJson(500, "数据有误", 0, data)
		}
		if user.Is_admin == 1 {
			c.ErrorJson(500, "超级管理员不支持删除", 0, data)
		}
		num = userService.UserDelete(id)
	}

	c.SuccessJson(0, data, int(num))
}

// 添加后台用户 struct
type AddAdminUserController struct {
	greenbee.AdminBaseController
}

// 添加后台用户页面
func (c *AddAdminUserController) Get() {
	//查询角色数据
	var roleService adminService.RoleService
	params := make(map[string]string)
	fields := make([]string, 0)
	fields = append(fields, "*")
	data, _, _ := roleService.GetList(params, fields)
	c.Data["role_data"] = data
	c.TplName = "admin/user/add.html"
}

// 添加用户
func (c *AddAdminUserController) Post() {
	var data interface{}
	username := c.GetString("username", "")
	status, _ := c.GetInt("status", 0)
	password := c.GetString("password", "")
	email := c.GetString("email", "")
	nickname := c.GetString("nickname", "")
	ids := c.GetString("role_ids", "")
	if username == "" {
		c.ErrorJson(500, "用户名不能为空", 0, data)
	}
	if password == "" {
		c.ErrorJson(500, "密码不能为空", 0, data)
	}
	if ids == "" {
		c.ErrorJson(500, "请选择角色", 0, data)
	}
	//判断用户账号是否已存在
	var userService adminService.AdminUserService
	var user models.AdminUser
	user.UserName = username
	field := []string{"UserName"}
	_, err := userService.GetUser(user, field)
	if err == nil {
		c.ErrorJson(500, "'"+username+"' 账号已存在", 0, data)
	}

	user.CreatedAt = greenbeeUtils.GetTodayFmtStr("2006-01-02 15:04:05", 0)
	user.UserName = username
	user.Status = int8(status)
	user.Password = greenbeeUtils.Sha1(greenbeeUtils.Md5(password))
	user.Email = email
	user.Nickname = nickname
	id := userService.UserAdd(user)
	if id > 0 {
		//角色分配
		roleIds := strings.Split(ids, ",")
		userService.UserRoleEmpower(int(id), roleIds)
		c.SuccessJson(0, data, 1)
	}
	c.ErrorJson(500, "添加失败", 0, data)

}

// 编辑后台用户 struct
type EditAdminUserController struct {
	greenbee.AdminBaseController
}

// 编辑用户 页面
func (c *EditAdminUserController) Get() {
	id, _ := c.GetInt("id", 0)

	//判断用户账号是否已存在
	var userService adminService.AdminUserService
	var user models.AdminUser
	user.Id = id
	field := []string{"Id"}
	user, err := userService.GetUser(user, field)
	if err != nil {
		c.Ctx.WriteString("数据有误")
		return
	}
	//查询角色数据
	var roleService adminService.RoleService
	params := make(map[string]string)
	fields := make([]string, 0)
	fields = append(fields, "*")
	data, _, _ := roleService.GetList(params, fields)
	//查询用户所属角色的id
	roleIds := userService.GetUserRoleIds(id)
	//处理角色选中状态
	type roleDatas struct {
		models.Role
		Ischecked bool
	}
	var roleData []roleDatas
	var role roleDatas
	if len(data) > 0 {
		for _, v := range data {
			role.Id = v.Id
			role.Label = v.Label
			role.Ischecked = greenbeeUtils.InArrayInt(v.Id, roleIds)
			roleData = append(roleData, role)
		}
	}
	c.Data["role_data"] = roleData
	c.Data["admin_user"] = user
	c.TplName = "admin/user/edit.html"
}

// 编辑用户
func (c *EditAdminUserController) Post() {
	var data interface{}
	id, _ := c.GetInt("id", 0)
	status, _ := c.GetInt("status", 0)
	password := c.GetString("password", "")
	email := c.GetString("email", "")
	nickname := c.GetString("nickname", "")
	ids := c.GetString("role_ids", "")
	if ids == "" {
		c.ErrorJson(500, "请选择角色", 0, data)
	}
	//判断用户账号是否存在
	var userService adminService.AdminUserService
	var user models.AdminUser
	user.Id = id
	field := []string{"Id"}
	_, err := userService.GetUser(user, field)
	if err != nil {
		c.ErrorJson(500, "参数有误", 0, data)
	}

	user.Status = int8(status)
	updateField := []string{"Email", "Nickname", "Status"}
	if password != "" {
		user.Password = greenbeeUtils.Sha1(greenbeeUtils.Md5(password))
		updateField = append(updateField, "Password")
	}
	user.Email = email
	user.Nickname = nickname
	//编辑用户
	userService.UserEdit(user, id, updateField)
	//角色分配
	roleIds := strings.Split(ids, ",")
	userService.UserRoleEmpower(id, roleIds)
	c.SuccessJson(0, data, 1)

}
